package com.lynx.system.controller;

import com.lynx.system.beans.Menu;
import com.lynx.system.beans.GlobalVO;
import com.lynx.system.beans.Type;
import com.lynx.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return this.port;
    }
    @GetMapping("/findAll/{index}/{limit}")
    public Object findAll(@PathVariable int index,@PathVariable int limit){
        List<Menu> menus = menuService.findAll(index, limit);
        int count = menuService.findCount();
        return new GlobalVO(0,"",count,menus);
    }
    @DeleteMapping("/deleteById/{id}")
    public Object deleteById(@PathVariable int id){
        menuService.deleteById(id);
        return new GlobalVO(0,"",1,null);
    }
    @GetMapping("/findTypes")
    public List<Type> findTyps(){
        List<Type> listTypes =menuService.getTypes();
        return listTypes;
    }
    @PostMapping("/save")
    public int save(@RequestBody Menu menu){
        return menuService.save(menu);
    }
    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable int id){
        Menu menu = menuService.findById(id);
        return menu;
    }
    @PostMapping("/edit")
    public int edit(@RequestBody Menu menu){
        return  menuService.edit(menu);
    }


}
